package com.sf.melon.utils;

import com.sf.melon.astar.AStar;
import com.sf.melon.astar.AStarResult;
import com.sf.melon.astar.MapInfo;
import com.sf.melon.astar.Node;
import com.sf.melon.model.Point;

import java.util.List;
import java.util.Map;

/**
 * Created by 854638 on 2017/12/1.
 */
public class LzhUtils {
    //根据去往的象限等乱七八糟的因素决定要去哪个点

    /**
     *
     * @param mapResult   地图信息
     * @param quadrantNo   象限编号
     * @return
     */
    public static Point getOptimalNode(Map<String,Object> mapResult, int quadrantNo) {
        Point ai = (Point) mapResult.get("ai");   //小哥当前位置
        int[][] map = buildMap(mapResult);
        int[][] blockMap = buildBlockMap(mapResult);
        int aiX = ai.getX();
        int aiY = ai.getY();
        int maxValue = 0;
        int maxValueX = 5;   //最开始认为它的最优点坐标在地图中心
        int maxValueY = 5;   //最开始认为它的最优点坐标在地图中心
        if(quadrantNo == 1) {
            for(int i = 0; i <= aiX; i++) {
                for(int j = aiY; j < 12; j++) {
                    int jobValue = map[i][j];
                    if(map[i][j] > 0) {
                        int realValue = jobValue - (i - aiX) - (j - aiY);
                        if(realValue > 0) {
                            MapInfo info = new MapInfo(blockMap, 12, 12, new Node(aiX, aiY), new Node(i, j));
                            AStarResult aStarResult = new AStar().start(info);
                            if(aStarResult != null){
                                int aStarPathVaule = aStarResult.getTotalPathValue();
                                Node node = aStarResult.getStart();
                                System.out.println("对于点 x: " + i + ", y: " + j + ", A* 最优路径：" + aStarPathVaule);
                                while (node != null) {
                                    System.out.print("(" + node.coord.x + ", "+ node.coord.y + ")" + " ->");
                                    node = node.child;
                                }
                                realValue = jobValue - aStarPathVaule;
                                if(maxValue < realValue) {
                                    maxValue = realValue;
                                    maxValueX = i;
                                    maxValueY = j;
                                    System.out.println("maxValue: " + maxValue);
                                }
                            }
                        }
                    }
                }
            }
        } else if(quadrantNo == 2) {
            for(int i = 0; i <= aiX; i++) {
                for(int j = 0; j <= aiY; j++) {
                    int jobValue = map[i][j];
                    if(map[i][j] > 0) {
                        int realValue = jobValue - (i - aiX) - (j - aiY);
                        if(realValue > 0) {
                            MapInfo info = new MapInfo(blockMap, 12, 12, new Node(aiX, aiY), new Node(i, j));
                            AStarResult aStarResult = new AStar().start(info);
                            if(aStarResult != null){
                                int aStarPathVaule = aStarResult.getTotalPathValue();
                                Node node = aStarResult.getStart();
                                System.out.println("对于点 x: " + i + ", y: " + j + ", A* 最优路径：" + aStarPathVaule);
                                while (node != null) {
                                    System.out.print("(" + node.coord.x + ", "+ node.coord.y + ")" + " ->");
                                    node = node.child;
                                }
                                realValue = jobValue - aStarPathVaule;
                                if(maxValue < realValue) {
                                    maxValue = realValue;
                                    maxValueX = i;
                                    maxValueY = j;
                                    System.out.println("maxValue: " + maxValue);
                                }
                            }
                        }
                    }
                }
            }
        } else if(quadrantNo == 3) {
            for(int i = aiX; i <12; i++) {
                for(int j = 0; j <=aiY; j++) {
                    int jobValue = map[i][j];
                    if(map[i][j] > 0) {
                        int realValue = jobValue - (i - aiX) - (j - aiY);
                        if(realValue > 0) {
                            MapInfo info = new MapInfo(blockMap, 12, 12, new Node(aiX, aiY), new Node(i, j));
                            AStarResult aStarResult = new AStar().start(info);
                            if(aStarResult != null){
                                int aStarPathVaule = aStarResult.getTotalPathValue();
                                Node node = aStarResult.getStart();
                                System.out.println("对于点 x: " + i + ", y: " + j + ", A* 最优路径：" + aStarPathVaule);
                                while (node != null) {
                                    System.out.print("(" + node.coord.x + ", "+ node.coord.y + ")" + " ->");
                                    node = node.child;
                                }
                                realValue = jobValue - aStarPathVaule;
                                if(maxValue < realValue) {
                                    maxValue = realValue;
                                    maxValueX = i;
                                    maxValueY = j;
                                    System.out.println("maxValue: " + maxValue);
                                }
                            }
                        }
                    }
                }
            }
        } else {
            for(int i = aiX; i < 12; i++) {
                for(int j = aiY; j <12; j++) {
                    int jobValue = map[i][j];
                    if(map[i][j] > 0) {
                        int realValue = jobValue - (i - aiX) - (j - aiY);
                        if(realValue > 0) {
                            MapInfo info = new MapInfo(blockMap, 12, 12, new Node(aiX, aiY), new Node(i, j));
                            AStarResult aStarResult = new AStar().start(info);
                            if(aStarResult != null){
                                int aStarPathVaule = aStarResult.getTotalPathValue();
                                Node node = aStarResult.getStart();
                                System.out.println("对于点 x: " + i + ", y: " + j + ", A* 最优路径：" + aStarPathVaule);
                                while (node != null) {
                                    System.out.print("(" + node.coord.x + ", "+ node.coord.y + ")" + " ->");
                                    node = node.child;
                                }
                                realValue = jobValue - aStarPathVaule;
                                if(maxValue < realValue) {
                                    maxValue = realValue;
                                    maxValueX = i;
                                    maxValueY = j;
                                    System.out.println("maxValue: " + maxValue);
                                }
                            }
                        }
                    }
                }
            }
        }
        Point point = new Point();
        point.setX(maxValueX);
        point.setY(maxValueY);
        return point;
    }

    /**
     *
     * @param mapResult   地图信息
     * @param quadrantNo   象限编号
     * @return
     */
    public static String getOptimalDirection(Map<String,Object> mapResult, int quadrantNo) {
        Point ai = (Point) mapResult.get("ai");   //小哥当前位置
        String direction = "D";
        int[][] map = buildMap(mapResult);
        int[][] blockMap = buildBlockMap(mapResult);
        int aiX = ai.getX();
        int aiY = ai.getY();
        int maxValue = 0;
        int maxValueX = 5;   //最开始认为它的最优点坐标在地图中心
        int maxValueY = 5;   //最开始认为它的最优点坐标在地图中心
        if(quadrantNo == 1) {
            for(int i = 0; i <= aiX; i++) {
                for(int j = aiY; j < 12; j++) {
                    int jobValue = map[i][j];
                    if(map[i][j] > 0) {
                        int realValue = jobValue - (i - aiX) - (j - aiY);
                        if(realValue > 0) {
                            MapInfo info = new MapInfo(blockMap, 12, 12, new Node(aiX, aiY), new Node(i, j));
                            AStarResult aStarResult = new AStar().start(info);
                            if(aStarResult != null){
                                int aStarPathVaule = aStarResult.getTotalPathValue();
                                Node node = aStarResult.getStart();
                                System.out.println("对于点 x: " + i + ", y: " + j + ", A* 最优路径：" + aStarPathVaule);
                                while (node != null) {
                                    System.out.print("(" + node.coord.x + ", "+ node.coord.y + ")" + " ->");
                                    node = node.child;
                                }
                                realValue = jobValue - aStarPathVaule;
                                if(maxValue < realValue) {
                                    maxValue = realValue;
                                    maxValueX = i;
                                    maxValueY = j;
                                    System.out.println("maxValue: " + maxValue);
                                }
                            }
                        }
                    }
                }
            }
        } else if(quadrantNo == 2) {
            for(int i = 0; i <= aiX; i++) {
                for(int j = 0; j <= aiY; j++) {
                    int jobValue = map[i][j];
                    if(map[i][j] > 0) {
                        int realValue = jobValue - (i - aiX) - (j - aiY);
                        if(realValue > 0) {
                            MapInfo info = new MapInfo(blockMap, 12, 12, new Node(aiX, aiY), new Node(i, j));
                            AStarResult aStarResult = new AStar().start(info);
                            if(aStarResult != null){
                                int aStarPathVaule = aStarResult.getTotalPathValue();
                                Node node = aStarResult.getStart();
                                System.out.println("对于点 x: " + i + ", y: " + j + ", A* 最优路径：" + aStarPathVaule);
                                while (node != null) {
                                    System.out.print("(" + node.coord.x + ", "+ node.coord.y + ")" + " ->");
                                    node = node.child;
                                }
                                realValue = jobValue - aStarPathVaule;
                                if(maxValue < realValue) {
                                    maxValue = realValue;
                                    maxValueX = i;
                                    maxValueY = j;
                                    System.out.println("maxValue: " + maxValue);
                                }
                            }
                        }
                    }
                }
            }
        } else if(quadrantNo == 3) {
            for(int i = aiX; i <12; i++) {
                for(int j = 0; j <=aiY; j++) {
                    int jobValue = map[i][j];
                    if(map[i][j] > 0) {
                        int realValue = jobValue - (i - aiX) - (j - aiY);
                        if(realValue > 0) {
                            MapInfo info = new MapInfo(blockMap, 12, 12, new Node(aiX, aiY), new Node(i, j));
                            AStarResult aStarResult = new AStar().start(info);
                            if(aStarResult != null){
                                int aStarPathVaule = aStarResult.getTotalPathValue();
                                Node node = aStarResult.getStart();
                                System.out.println("对于点 x: " + i + ", y: " + j + ", A* 最优路径：" + aStarPathVaule);
                                while (node != null) {
                                    System.out.print("(" + node.coord.x + ", "+ node.coord.y + ")" + " ->");
                                    node = node.child;
                                }
                                realValue = jobValue - aStarPathVaule;
                                if(maxValue < realValue) {
                                    maxValue = realValue;
                                    maxValueX = i;
                                    maxValueY = j;
                                    System.out.println("maxValue: " + maxValue);
                                }
                            }
                        }
                    }
                }
            }
        } else {
            for(int i = aiX; i < 12; i++) {
                for(int j = aiY; j <12; j++) {
                    int jobValue = map[i][j];
                    if(map[i][j] > 0) {
                        int realValue = jobValue - (i - aiX) - (j - aiY);
                        if(realValue > 0) {
                            MapInfo info = new MapInfo(blockMap, 12, 12, new Node(aiX, aiY), new Node(i, j));
                            AStarResult aStarResult = new AStar().start(info);
                            if(aStarResult != null){
                                int aStarPathVaule = aStarResult.getTotalPathValue();
                                Node node = aStarResult.getStart();
                                System.out.println("对于点 x: " + i + ", y: " + j + ", A* 最优路径：" + aStarPathVaule);
                                while (node != null) {
                                    System.out.print("(" + node.coord.x + ", "+ node.coord.y + ")" + " ->");
                                    node = node.child;
                                }
                                realValue = jobValue - aStarPathVaule;
                                if(maxValue < realValue) {
                                    maxValue = realValue;
                                    maxValueX = i;
                                    maxValueY = j;
                                    System.out.println("maxValue: " + maxValue);
                                }
                            }
                        }
                    }
                }
            }
        }
//        Point point = new Point();
//        point.setX(maxValueX);
//        point.setY(maxValueY);
//        return point;
        MapInfo info = new MapInfo(blockMap, 12, 12, new Node(aiX, aiY), new Node(maxValueX, maxValueY));
        AStarResult aStarResult = new AStar().start(info);
        Node start = aStarResult.getStart();
        System.out.println(start.coord.x + ", " + start.coord.y + ", " + aiX + ", " + aiY);
        if(start.coord.x > aiX) {
            direction = "D";
        } else if(start.coord.x < aiX) {
            direction = "U";
        } else if(start.coord.y > aiY) {
            direction = "R";
        } else {
            direction = "L";
        }
        System.out.println("下一步该走的方向：" + direction);
        return direction;
    }

    /**
     * 组建地图
     * 有价值的点值为价值，障碍物的点值为-1，其他点为0
     */
    public static int[][] buildMap(Map<String,Object> mapResult) {
        int[][] result = new int[12][12];
        List<Point> jobs = (List<Point>) mapResult.get("jobs");
        List<Point> walls = (List<Point>) mapResult.get("walls");
        for(Point job : jobs) {
            result[job.getX()][job.getY()] = job.getValue();
        }
        for (Point wall : walls) {
            result[wall.getX()][wall.getY()] = -1;
        }
        return result;
    }

    /**
     * 组建障碍物地图
     * 有障碍物点值为1
     */
    public static int[][] buildBlockMap(Map<String,Object> mapResult) {
        int[][] result = new int[12][12];
        List<Point> walls = (List<Point>) mapResult.get("walls");
        for (Point wall : walls) {
            result[wall.getX()][wall.getY()] = AStar.BAR;
        }
        return result;
    }
}
